const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const {formatTime_2} = require('../../utils/util.js');
Page({
  data: {
    title:'',
    role:'',
    content:'',
    date1:'',
    date2:'',
    id:'',
    resumeitem:'',
  },
  onLoad: function (options) {
    if(options.id){
      this.setData({
        id:options.id,
        resumeitem:app.globalData.resumeitem
      })
      this.getdetail()
      wx.setNavigationBarTitle({
        title: '修改项目经历' 
      })
    }
  },
  getdetail(){
    var detail=this.data.resumeitem
    this.setData({
      title:detail.title,
      role:detail.role,
      content:detail.content,
      date1:formatTime_2(detail.startDate),
      date2:formatTime_2(detail.endDate),
    })
    app.globalData.resumeitem=''
  },
  bindPickerChange1: function(e) {
    this.setData({
      date1: e.detail.value
    })
  },
  bindPickerChange2: function(e) {
    this.setData({
      date2: e.detail.value
    })
  },
  changeinput(e){
    let data = {};
    data[e.currentTarget.dataset.name]=e.detail.value
    this.setData(data)
  },
  save(){
    if(!this.data.title){
      wx.showToast({
        title:"请填写项目名称",
        icon:"none"
      })
      return
    }
    if(!this.data.role){
      wx.showToast({
        title:"请填写担任角色",
        icon:"none"
      })
      return
    }
    if(!this.data.content){
      wx.showToast({
        title:"请填写项目描述",
        icon:"none"
      })
      return
    }
    if(!this.data.date1){
      wx.showToast({
        title:"请选择开始时间",
        icon:"none"
      })
      return
    }
    if(!this.data.date2){
      wx.showToast({
        title:"请选择结束时间",
        icon:"none"
      })
      return
    }
    var data={
      title:this.data.title,
      role:this.data.role,
      content:this.data.content,
      startDate:this.data.date1,
      endDate:this.data.date2,
    }
    if(this.data.id){
      data.userItemId=this.data.id
      Controller.updateUserItem(data).then(res=>{
        wx.showToast({
          title:"修改成功",
          icon: 'success',
          duration: 2000
        })
        setTimeout(res => {
          wx.navigateBack({ //返回
            delta: 1
          })
        },2000)
      })
    }else{
      Controller.addUserItem(data).then(res=>{
        wx.showToast({
          title:"添加成功",
          icon: 'success',
          duration: 2000
        })
        setTimeout(res => {
          wx.navigateBack({ //返回
            delta: 1
          })
        },2000)
      })
    }
    
  },
  delete(){
    Controller.deleteUserItem({userItemId:this.data.id}).then(res=>{
      wx.showToast({
        title:"删除成功",
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      },2000)
    })
  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})